package com.woniu.defecttracking.controller.datas;


import com.woniu.defecttracking.commens.Result;
import com.woniu.defecttracking.exception.AccountException;
import com.woniu.defecttracking.exception.RoleException;
import com.woniu.defecttracking.param.UpdateAccountRoleParam;
import com.woniu.defecttracking.service.AccountRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>

 * @author 倪西
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/accountRole")
public class AccountRoleController {

    @Autowired
    private AccountRoleService accountRoleService;

    /**
    * @作者: 倪西
    * @描述：通过账号查询对应的角色id
    * @时间：2022/1/7 21:41
    * @参数：[id]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @GetMapping("/getRoleIds/{id}")
    public Result getRoleIds(@PathVariable("id") Integer id){
        List<Integer> roleIds = accountRoleService.getRoleIds(id);
        return Result.getInstance(roleIds).setMessage("查询成功");
    }

    /**
    * @作者: 倪西
    * @描述：修改账号对应的角色授权
    * @时间：2022/1/7 21:41
    * @参数：[updateAccountRoleParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("/saveAccountRoles")
    public Result saveAccountRoles(@RequestBody UpdateAccountRoleParam updateAccountRoleParam) throws AccountException, RoleException {
        Boolean bool = accountRoleService.saveAccountRoles(updateAccountRoleParam);
        if (bool){
            return Result.getInstance(null).setMessage("授权成功");
        }else {
            return Result.getInstance(null).setMessage("授权失败").setCode(422);
        }
    }
}

